﻿using Core;
using System.Net;

namespace BGYMqttServer
{
    /// <summary>
    /// 监听选项
    /// </summary>
    public class ListenOptions : IConfigureOptions
    {
        /// <summary>
        /// http
        /// </summary>
        public IPEndpoint Http { get; set; } = new IPEndpoint();

        /// <summary>
        /// mqtt
        /// </summary>
        public IPEndpoint Mqtt { get; set; } = new IPEndpoint();


        /// <summary>
        /// ip和端口
        /// </summary>
        public class IPEndpoint
        {
            /// <summary>
            /// ip地址
            /// </summary>
            public string IPAddress { get; set; } = System.Net.IPAddress.Any.ToString();

            /// <summary>
            /// 端口
            /// </summary>
            public int Port { get; set; }

            /// <summary>
            /// 返回ip地址
            /// </summary>
            /// <returns></returns>
            public IPAddress GetIPAddress()
            {
                if (System.Net.IPAddress.TryParse(this.IPAddress, out var ip))
                {
                    return ip;
                }
                return System.Net.IPAddress.Any;
            }
        }
    }
}
